package framework;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import JassTranslator.ImportJass;
import JassTranslator.JassLineTranslator;

public abstract class Translator {
	protected ArrayList<Class<? extends JassLineTranslator>> lineTranslators = new ArrayList<Class<? extends JassLineTranslator>>();
	
	@SuppressWarnings("unchecked")
	public Translator(){
		for(Class c : this.getClass().getClasses()){
			if(JassLineTranslator.class.isAssignableFrom(c)){
				lineTranslators.add(c);
			}
		}
	}
	
	public List<String> translateLine(String codeString) throws InstantiationException, IllegalAccessException, JassSyntaxException, ClassNotFoundException {
		boolean foundTranslator = false;
		JassLineTranslator currentTranslator = null;
		for(Class<? extends JassLineTranslator> c : lineTranslators){
			currentTranslator = c.newInstance();
			if(currentTranslator.isLineOfType(StringUtils.trim(codeString))) {
				foundTranslator = true;
				break;
			}
		}
		if(foundTranslator){
			currentTranslator.checkLineForSyntaxErrors();
			return currentTranslator.translateLineList(codeString);
		}
		return TranslatorUtils.stringToList(codeString);
	}
	
	public abstract String getImportFileExtension();
	
	public abstract String getExportFileExtension();

}
